﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization;

namespace Improve157
{
    public class Chapter_01
    {
        public void Suggestion_1()
        {

        }

        public void Suggestion_02()
        {
            IPAddress ip = IPAddress.Parse("192.168.1.1");

            IPEndPoint iPEndPoint = new IPEndPoint(ip, 50);
            Animal animal1 = new Animal();
            Dog dog1 = new Dog();

            Animal animal2 = dog1;

            Dog dog2 = (Dog)animal1;

            Dog do3 = animal1 as Dog;


        }

        /// <summary>
        /// 05 使用Int?来确保值类型也可以为null
        /// </summary>
        public void Suggestion_05()
        {
            int i = 1;
            int? i2 = i;
            if (i2.HasValue)
            {
                int i3 = i2.Value;
            }


            int i4 = i2 ?? 0;
        }

        const int i2 = 10;
        readonly List<int> TestList = new List<int>();
        readonly int intR;
        public void Suggestion_06()
        {
            TestList.Add(01);
            //intR = 12;

        }

        public void Suggestion_07()
        {
            Console.WriteLine((int)Day.Two + "   " + (int)Day.Three  +"   " + (int)Day.Six);
        }

        enum Day: int 
        {
            One = 2,
            Two = 120,
            Three,
            Four = 5,
            Five = 110,
            Six,
        }
           
    }

    public class Animal : ICloneable
    {
        public object Clone()
        {
            return this.MemberwiseClone();
        }
    

        public void newMethod1()
        {
            Console.WriteLine("我是爹");
        }

        public virtual void overrideMethod()
        {
            Console.WriteLine("我是爹");
        }
    }

    public class Dog : Animal,IFormattable
    {
        public int Age { get; set; }
        public new void newMethod1()
        {
            Console.WriteLine("我是儿子");
        }

        public override void overrideMethod()
        {
            Console.WriteLine("我是儿子");
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            string _Result = string.Empty;
            switch (format)
            {
                case "value1":
                    _Result = "value1";
                    break;
                case "value2":
                    _Result = "value2";
                    break;
                default:
                    _Result = "default";
                    break;
            }

            return _Result;
        }

        public override string ToString()
        {
            return base.ToString();
        }

        public static Dog operator +(Dog dog1 ,Dog dog2)
        {
            dog2.Age += dog1.Age;
            return dog2;
        }
    }

    class DofFomatter : IFormatProvider, ICustomFormatter
    {
        public object GetFormat(Type formatType)
        {
            if (formatType == typeof(ICustomFormatter))
            {
                return this;
            }
            else
            {
                return null;
            }
        }

        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            Dog dog = arg as Dog;
            if (dog == null)
            {
                return string.Empty;
            }

            string _Result = string.Empty;
            switch (format)
            {
                case "value1":
                    _Result = "value1";
                    break;
                case "value2":
                    _Result = "value2";
                    break;
                default:
                    _Result = "default";
                    break;
            }

            return _Result;
        }
    }
}
